package com.jpaAutoProcess;

import com.jpaAutoProcess.selectKernel.KernelClass;
import com.jpaAutoProcess.utils.parseParamCondition.RequirParametersAndConditions;
import com.jpaAutoProcess.utils.ParamConditionBO;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cc
 * @date 2022/6/18 17:35
 * @Description : selec自动拼接语句
 */

public class AutoSelect<T> {


    /**
     *
     * @param select (select xxx ,xxx From 注意：尽量别写select *)
     * @param orderBy 根据哪个个排序 (与字段名对应)
     * @param from 条件 (from xxx )
     * @param order 倒序desc 还是顺序asc，可以为空 (与字段名对应)
     * @param ParametersAndConditionsMap 使用：RequirParametersAndConditions.getInstance() 需要判断的字段和判断条件 类型为AutoPageParametersConditionsMap
     * @param entityManager sql执行
     * @param classtype 要转换成什么类型
     **/
    public static Object selectOne(StringBuilder select, StringBuilder from, String orderBy, String order, RequirParametersAndConditions<ParamConditionBO, Object> ParametersAndConditionsMap, EntityManager entityManager, Class<?> classtype){

        return selectPortal(select.toString(),from.toString(),orderBy,order,ParametersAndConditionsMap,entityManager,classtype,false);
    }

    /**
     *
     * @param select (select xxx ,xxx From 注意：尽量别写select *)
     * @param orderBy 根据哪个个排序 (与字段名对应)
     * @param from 条件 (from xxx )
     * @param order 倒序desc 还是顺序asc，可以为空 (与字段名对应)
     * @param databaseParameters 使用：RequirParametersAndConditions.getInstance() 需要判断的字段和判断条件 类型为AutoPageParametersConditionsMap
     * @param entityManager sql执行
     * @param classtype 要转换成什么类型
     **/
    public static List<?> selectList(StringBuilder select, StringBuilder from, String orderBy, String order, RequirParametersAndConditions<ParamConditionBO, Object> databaseParameters, EntityManager entityManager, Class<?> classtype){
        return (List<?>)selectPortal(select.toString(),from.toString(),orderBy,order,databaseParameters,entityManager,classtype,true);
    }

    /**
     *
     * @param select (select xxx ,xxx From 注意：尽量别写select *)
     * @param orderBy 根据哪个个排序 (与字段名对应)
     * @param from 条件 (from xxx )
     * @param order 倒序desc 还是顺序asc，可以为空 (与字段名对应)
     * @param databaseParameters 需要判断的字段和判断条件 类型为AutoPageParametersConditionsMap 直接RequirParametersAndConditions.getInstance()
     * @param entityManager sql执行
     * @param classtype 要转换成什么类型
     * @versioon 1.3.1
     **/
    private static Object selectPortal (String select, String from, String orderBy, String order, RequirParametersAndConditions<ParamConditionBO, Object> databaseParameters, EntityManager entityManager, Class<?> classtype,boolean isList){
        StringBuilder selectSql = new StringBuilder().append(select);
        StringBuilder fromsql = new StringBuilder().append(from);
        StringBuilder wheresql = new StringBuilder();
        Map<String, Object> params = new HashMap<String, Object>();

        //拼接sql 和放置where即params参数
        KernelClass.spliceSentencesAndPutParams(databaseParameters,wheresql,params);
        //进行查询
        //判断是否需要排序
        if (orderBy!=null && !orderBy.equals("")){
            wheresql.append(" ORDER BY ").append(orderBy).append(" ");
            if (order !=null && !order.equals("")){
                wheresql.append(order);
            }
        }
        Object getresultlist = KernelClass.getresultlist(selectSql, fromsql, wheresql, classtype, entityManager, params,isList);
//        Object getresultlist = KernelClass.getresultlist();
        return getresultlist;
    }


}
